import path from "path";
import { existsSync, rmSync } from "fs";
import inquirer from "inquirer";
import { loading } from "../utils/loading.js";
export async function create(name, opt) {
  const cwd = process.cwd();
  const targetDir = path.join(cwd, name);
  if (existsSync(targetDir)) {
    if (opt.force) {
      rmSync(targetDir, { recursive: true });
    } else {
      const { action } = await inquirer.prompt([
        {
          type: "list",
          name: "action",
          message: "项目已存在！是否覆盖？",
          choices: [
            { name: "overwrite", value: "overwrite" },
            { name: "cancel", value: false },
          ],
        },
      ]);
      if (!action) {
        return console.log("已取消创建");
      }
      if (action === "overwrite") {
        await loading("overwrite......", () => {
          rmSync(targetDir, { recursive: true });
        });
        console.log("overwrite complete!");
      }
      console.log(action);
    }
  }
}
